import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutterdemo/day11_event/user_model.dart';

final eventBus = EventBus();

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // 启动要显示的界面
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Evenet使用示例"),
      ),
      body: MyHomeBody(),
    );
  }
}

class MyHomeBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
     child: Column(
       mainAxisAlignment: MainAxisAlignment.center,
       children: [
         JKButton(),
         JKText()
       ],
     ),
    );
  }
}

class JKButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: () {
        print('点击事件');
        final info = UserModel('wc', 28);
        eventBus.fire(info);
      },
      child: Text('点击'),
    );
  }
}

class JKText extends StatefulWidget {
  @override
  _JKTextState createState() => _JKTextState();
}

class _JKTextState extends State<JKText> {
  String _message = 'Hello World!';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    eventBus.on<UserModel>().listen((data) {
      setState(() {
        _message = '${data.nickname} ${data.age}';
      });

    });

  }

  @override
  Widget build(BuildContext context) {
    return Text('$_message', style: TextStyle(fontSize: 22, color: Colors.green),);
  }
}

